//
//  ShopDetailsViewController.m
//  1243
//
//  Created by 杨森 on 16/4/28.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ShopDetailsViewController.h"
#import "ShopOneCollectionViewCell.h"

#define MARGIN_WIDTH 5

@interface ShopDetailsViewController ()

@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;

@end

@implementation ShopDetailsViewController

static NSString * const reuseIdentifier = @"shopDetailsCell";

- (instancetype)init {
    
    self = [super init];
    
    if (self) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.flowLayout = flowLayout;
        
        self = [self initWithCollectionViewLayout:flowLayout];
    }
    
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupCollectionView];
}

- (void)setupCollectionView
{
    [self.collectionView registerNib:[UINib nibWithNibName:@"ShopOneCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:reuseIdentifier];
    
    _flowLayout.itemSize = CGSizeMake(WEITH/2 - MARGIN_WIDTH, HEIGHT/2.5 + 20);
    _flowLayout.minimumLineSpacing = MARGIN_WIDTH;
    _flowLayout.minimumInteritemSpacing = MARGIN_WIDTH;
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView.backgroundColor = BACKGROUND_COLOR;
    
    //注册表头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}

#pragma mark >>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShopOneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.ShopImage.image = [UIImage imageNamed:@"shopZan"];
    cell.shopNameLabel.text = @"夏季新品白色无袖半袖";
    cell.saleLabel.text = @"已售65";
    cell.priceLabel.text = @"¥119.4";
    
    return cell;
}

#pragma mark---加载UICollectionView表头
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    if ([collectionView isEqual:self.collectionView]) {
        
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { // header
            reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
            
            [self creatHeaderView:reusableView];
            
            reusableView.backgroundColor = [UIColor whiteColor];
            
            return reusableView;
        }
        
    }
    
    return nil;
    
}

- (void)creatHeaderView:(UICollectionReusableView *)view {
    
    NSArray *arr = @[@"综合", @"销量", @"价格"];
    for (int i = 0; i < 3; i ++) {
        UIButton *btn = [[UIButton alloc] init];
        
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        
        
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        btn.layer.masksToBounds = YES;
        btn.layer.borderColor  = [UIColor blackColor].CGColor; //要设置的颜色
        btn.layer.borderWidth = 0.5; //要设置的描边宽
        
        btn.tag = 1000 + i;
        
        [btn setFrame:CGRectMake(WIDTH / 3 * i, 0, WIDTH / 3, 44)];
        
        [view addSubview:btn];
    }
}



//header组头大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(WEITH, 44);
}


@end
